<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账号编码" prop="accCode">
        <el-input
          v-model="queryParams.accCode"
          placeholder="请输入账号编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据维度" prop="bar">
        <el-input
          v-model="queryParams.bar"
          placeholder="请输入数据维度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="所属用户" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入账号所属用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择账号状态" clearable>
          <el-option
            v-for="dict in dict.type.okx_is_use"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bussiness:cofig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bussiness:cofig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bussiness:cofig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bussiness:cofig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cofigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="账号信息" align="center" >
        <template slot-scope="scope">
          <div style="text-align: center; line-height: 1.5;">
            <div style="text-align: left; display: inline-block; line-height: 1.5;">
              <div >名称:{{ scope.row.accName }} <span style="color: red;">|</span>维度:{{ scope.row.bar }}</div>
              <div >编码:{{ scope.row.accCode }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="交易设置" align="center" >
        <template slot-scope="scope">
          <div style="text-align: center; line-height: 1.5;">
            <div style="text-align: left; display: inline-block; line-height: 1.5;">
              <div > 止盈率:{{ scope.row.zysyl }}<span style="color: red;">|</span>止损率:{{ scope.row.zssyl }} </div>
              <div >
                阈值:{{ scope.row.monitorYz }}<span style="color: red;">*</span>{{ scope.row.rate }}
                <span style="color: red;">|</span>
                杠:{{ scope.row.lever }}倍<span style="color: red;">|</span>数:{{ scope.row.tradeNum }}
              </div>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="资金信息" align="center" >
        <template slot-scope="scope">
          <div style="text-align: center; line-height: 1.5;">
            <div style="text-align: left; display: inline-block; line-height: 1.5;">
              <div>
                本金:{{ scope.row.capital }}<span style="color: red;">|</span>
                实时:{{ scope.row.balance }}<span style="color: red;">|</span>
                <span v-if="scope.row.netGain >= 0">
                  <span style="color: green;">赚{{ scope.row.netGain }}U</span> </span>
                      <span v-else>
                  <span style="color: red;">亏{{ Math.abs(scope.row.netGain) }}U</span>
                </span>
              </div>
              <div>备注:{{ scope.row.remark }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="用户信息" align="center" >
        <template slot-scope="scope">
          <div style="text-align: center; line-height: 1.5;">
            <div style="text-align: left; display: inline-block; line-height: 1.5;">
              <div >委托:{{ scope.row.dealNotify }}<span style="color: red;">|</span>平仓:{{ scope.row.closeNotify }}</div>
              <div >所属用户:{{ scope.row.userName }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="测试账号" align="center" prop="testFlat">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.okx_account_test_flat" :value="scope.row.testFlat"/>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.okx_is_use" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bussiness:cofig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bussiness:cofig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改账号配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账号名称" prop="accName">
          <el-input v-model="form.accName" placeholder="请输入账号名简称" />
        </el-form-item>
        <el-form-item label="账号编码" prop="accCode">
          <el-input v-model="form.accCode" placeholder="请输入账号编码" />
        </el-form-item>
        <el-form-item label="账号key" prop="apiKey">
          <el-input v-model="form.apiKey" placeholder="请输入欧意ApiKey" />
        </el-form-item>
        <el-form-item label="账号密码" prop="passWord">
          <el-input v-model="form.passWord" placeholder="请输入欧意密码" />
        </el-form-item>
        <el-form-item label="私钥Key" prop="secretKey">
          <el-input v-model="form.secretKey" placeholder="请输入欧意SecretKey" />
        </el-form-item>
        <el-form-item label="数据维度" prop="bar">
          <el-input v-model="form.bar" placeholder="请输入数据维度" />
        </el-form-item>
        <el-form-item label="测试账号" prop="testFlat">
          <el-radio-group v-model="form.testFlat">
            <el-radio
              v-for="dict in dict.type.okx_account_test_flat"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属用户" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入所属用户" />
        </el-form-item>
        <el-form-item label="账号状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.okx_is_use"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="账号备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入账号备注信息" />
        </el-form-item>
        <el-form-item label="收益" prop="netGain">
          <el-input v-model="form.netGain" placeholder="请输入收益" />
        </el-form-item>
        <el-form-item label="本金" prop="capital">
          <el-input v-model="form.capital" placeholder="请输入本金" />
        </el-form-item>
        <el-form-item label="止盈率" prop="zysyl">
          <el-input v-model="form.zysyl" placeholder="请输入止盈收益率" />
        </el-form-item>
        <el-form-item label="止损率" prop="zssyl">
          <el-input v-model="form.zssyl" placeholder="请输入止损收益率" />
        </el-form-item>
        <el-form-item label="监控阈值" prop="monitorYz">
          <el-input v-model="form.monitorYz" placeholder="请输入监控阈值" />
        </el-form-item>
        <el-form-item label="阈值比例" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入阈值百分比" />
        </el-form-item>
        <el-form-item label="杠杆倍数" prop="lever">
          <el-input v-model="form.lever" placeholder="请输入杠杆倍数" />
        </el-form-item>
        <el-form-item label="下单数量" prop="tradeNum">
          <el-input v-model="form.tradeNum" placeholder="请输入下单数量" />
        </el-form-item>
        <el-form-item label="委托通知" prop="dealNotify">
          <el-input v-model="form.dealNotify" placeholder="请输入委托单成交通知" />
        </el-form-item>
        <el-form-item label="平仓通知" prop="closeNotify">
          <el-input v-model="form.closeNotify" placeholder="请输入平仓通知" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCofig, getCofig, delCofig, addCofig, updateCofig } from "@/api/bussiness/cofig"

export default {
  name: "Cofig",
  dicts: ['okx_is_use', 'okx_account_test_flat'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账号配置表格数据
      cofigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accName: null,
        accCode: null,
        apiKey: null,
        passWord: null,
        secretKey: null,
        bar: null,
        testFlat: null,
        userName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询账号配置列表 */
    getList() {
      this.loading = true
      listCofig(this.queryParams).then(response => {
        this.cofigList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        accName: null,
        accCode: null,
        apiKey: null,
        passWord: null,
        secretKey: null,
        bar: null,
        testFlat: null,
        userName: null,
        status: null,
        remark: null,
        netGain: null,
        capital: null,
        balance: null,
        zysyl: null,
        zssyl: null,
        monitorYz: null,
        rate: null,
        lever: null,
        tradeNum: null,
        closeNotify: null,
        dealNotify: null,
        addTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加账号配置"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCofig(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改账号配置"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCofig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCofig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除账号配置编号为"' + ids + '"的数据项？').then(function() {
        return delCofig(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bussiness/cofig/export', {
        ...this.queryParams
      }, `cofig_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
